//
//  FileViewController.m
//  OAProject
//
//  Created by yhj on 2017/3/13.
//  Copyright © 2017年 cdnunion. All rights reserved.
//  附件主界面

#import "FileViewController.h"
#import "FileTableViewCell.h"
#import "FileListModel.h"
#import "NHNetworkHelper.h"
#import "MBProgressHUD+NH.h"
//#import "XLPhotoBrowser.h"

static NSString *tableViewCellIdentifier=@"tableViewCellIdentifier";
@interface FileViewController ()<UITableViewDataSource,UITableViewDelegate,FileTableViewCellDelegate>
@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *fileListArr;

@property(nonatomic,strong)UIView *bgView;

@property(nonatomic,strong)NSArray *array;
@end

@implementation FileViewController

// delegate
-(void)showImageFullWithFileListModel:(FileListModel *)model cell:(FileTableViewCell *)cell
{
    NSLog(@"sdhfhdfh---%@",model.url);
    _array=@[model.url];

    // 快速创建并进入浏览模式
//    XLPhotoBrowser *browser=[XLPhotoBrowser showPhotoBrowserWithCurrentImageIndex:0 imageCount:_array.count datasource:self];
//
//    // 设置长按手势弹出的地步ActionSheet数据,不实现此方法则没有长按手势
//    [browser setActionSheetWithTitle:@"这是一个类似微信/微博的图片浏览器组件" delegate:self cancelButtonTitle:nil deleteButtonTitle:@"删除" otherButtonTitles:@"发送给朋友",@"保存图片",@"收藏",@"投诉",nil];
//
//    // 自定义pageControl的一些属性
//    browser.pageDotColor = [UIColor purpleColor]; ///< 此属性针对动画样式的pagecontrol无效
//    browser.currentPageDotColor = [UIColor greenColor];
//    browser.pageControlStyle = XLPhotoBrowserPageControlStyleClassic;///< 修改底部pagecontrol的样式为系统样式,默认是弹性动画的样式

//    XLPhotoBrowserController *vc=[XLPhotoBrowserController new];
//    [vc setupUIWithCurrentImgAdapter:array.firstObject imageAdaptersBank:array];
//    [self.navigationController pushViewController:vc animated:NO];

//    UIView *bgView=[[UIView alloc]initWithFrame:self.view.bounds];
//    bgView.backgroundColor=[UIColor redColor];
//    [self.view addSubview:bgView];
//    self.bgView=bgView;
//
//    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent)];
//    [bgView addGestureRecognizer:tap];

}

//- (UIImage *)photoBrowser:(XLPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
//{
//    return self.array[index];
//}
//
//- (UIView *)photoBrowser:(XLPhotoBrowser *)browser sourceImageViewForIndex:(NSInteger)index
//{
//    return self.scrollView.subviews[index];
//}


-(void)tapEvent
{
    [self.bgView removeFromSuperview];
}

// delegate
-(void)downLoadFileWithFileListModel:(FileListModel *)model cell:(FileTableViewCell *)cell
{
    MBProgressHUD *hud=[MBProgressHUD showProgressToView:nil ProgressModel:MBProgressHUDModeDeterminate Text:@"下载中"];

    [[NHNetworkHelper shareInstance] downloadFileWithRequestUrl:model.url Parameter:nil SavedPath:nil Complete:^(NSData *data, NSError *error) {

          [hud hideAnimated:NO];

        if (error)
        {
            [MBProgressHUD showError:@"下载失败" ToView:self.view];
        }
        else
        {
            [MBProgressHUD showSuccess:@"下载完成" ToView:self.view];
        }
    } Progress:^(id downloadProgress, double currentValue) {
        hud.progress = currentValue;
    }];
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title=@"附  件";

    _fileListArr=[NSMutableArray array];

    [self.view addSubview:self.tableView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self GetData];
}

-(void)GetData
{
    self.tableView.mj_header=[MJRefreshNormalHeader
                              headerWithRefreshingBlock:^{
                                  [self getFileListData];
                              }];
    [self.tableView.mj_header beginRefreshing];
}


-(void)getFileListData
{
    [_fileListArr removeAllObjects];

    // 数据源
    NSMutableArray *arr=[NSMutableArray array];

    if (self.fileArr.count>0)
    {
        for (NSDictionary *dic in self.fileArr)
        {
            FileListModel *model=[FileListModel yy_modelWithDictionary:dic];
            [arr addObject:model];
        }
        self.fileListArr=arr;

        [self.tableView reloadData];
    }
    else
    {
        [self.view makeToast:@"暂无附件" duration:1 position:CSToastPositionCenter];
    }
    [self.tableView.mj_header endRefreshing];

}


// tableView
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH) style:UITableViewStylePlain];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=MAIN_BACK_COLOR;
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.tableFooterView=[UIView new];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
        [self.tableView registerClass:[FileTableViewCell class] forCellReuseIdentifier:tableViewCellIdentifier];
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.fileListArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FileTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
    cell.delegate=self;
    cell.model=self.fileListArr[indexPath.row];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kmargin*8;
}

// 间隙置边
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


@end
